package com.hnust.controller;

import com.hnust.domain.record.AdminRecord;
import com.hnust.domain.record.CourseRecord;
import com.hnust.service.LoginService;
import com.hnust.util.Result;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *创建人：张汝往
 *创建时间：2020/10/6
 *描述：登陆模块
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/api/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 管理员登录界面，输入账号和密码和身份登录系统
     * @param map 返回前端数据（account 账号，password 密码，identity 身份）
     * @return
     */
    @PostMapping("/collegeAdmin")
    public Result<AdminRecord> adminLogin(@RequestBody Map<String,Object> map){
        //System.out.println(map);
        try{
            String account=map.get("username").toString();
            String password=map.get("password").toString();
            String identity=map.get("identity").toString();
            //System.out.println(account);
            AdminRecord adminInfo=new AdminRecord();
            if(identity.equals("院管理员")){
                adminInfo=loginService.collegeAdminLogin(account,password);
            }else{
                adminInfo=loginService.schoolAdminLogin(account,password);
            }
            if(adminInfo==null){
                return new Result<AdminRecord>(0,"账号或密码错误",adminInfo);
            }else{
                return new Result<AdminRecord>(1,"登录成功",adminInfo);
            }
        }catch (Exception e){
            return new Result<AdminRecord>(0,e.getMessage().toString(),null);
        }

    }

}
